package com.zufe.stringbuilderdemo;

import java.util.StringJoiner;

public class demo3 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        String str = arrToString2(arr);
        System.out.println(str);
    }
    public static String arrToString(int[] arr) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for(int i = 0; i < arr.length; i++){
            if(i == arr.length - 1){
                sb.append(arr[i]);
            }else{
                sb.append(arr[i]).append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static String arrToString2(int[] arr) {
        StringJoiner sj = new StringJoiner(",", "[", "]");
        for (int i = 0; i < arr.length; i++) {
            sj.add(String.valueOf(arr[i]));
        }
        return sj.toString();
    }
}
